package com.avic.rong.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.avic.rong.R;
import com.avic.rong.payUtil.pay.WexinAliPayRsultCallBack;
import com.avic.rong.utils.ConstantsUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	private static final String TAG = "WXPayEntryActivity";
	private static WexinAliPayRsultCallBack mPayRsultCallBack;
	private  static String wxAppPayId;
	public static void setmPayRsultCallBack(String payId, WexinAliPayRsultCallBack mPayRsultCallBack) {
		WXPayEntryActivity.mPayRsultCallBack = mPayRsultCallBack;
		WXPayEntryActivity.wxAppPayId = payId;
	}
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, ConstantsUtils.APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	/*@Override
	public void onResp(BaseResp resp) {
		System.out.println("--------------onPayFinish, errCode = " + resp.errCode);

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.app_tip + resp.getType() + "--" + resp.errCode);
			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
			builder.show();
			int payStatus = 1;//1:成功 ,2失败
			if (resp.errCode == 0){
				payStatus = 1;
			}else{
				payStatus = 2;
			}
			startActivity(PayResultActivity.queryIntent(WXPayEntryActivity.this,AppUtils.BASE_URL + "app/pay/payFinish2?orderId=" + MyApplication.getInstance().orderId + "&payStatus=" + payStatus));
		}
	}*/
	@Override
	public void onResp(BaseResp resp) {
		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			int code = resp.errCode;
			switch (code) {
				case 0:
					// 成功
					if (mPayRsultCallBack != null) {
						mPayRsultCallBack.onSucceed();
					}
					break;
				case -1:
					// 可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等
					if (mPayRsultCallBack != null) {
						mPayRsultCallBack.onFailure();
					}
					break;
				case -2:
					// 用户取消
					if (mPayRsultCallBack != null) {
						mPayRsultCallBack.onCancel();
					}
					break;

				default:
					break;
			};
			finish();
		}

	}
}